<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/22
 * Time: 11:27
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\business\BusSearch;
use app\common\business\BusSearchUser;
use app\common\model\BuddhaBanner;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MallBanner;
use app\common\model\MallSearchUser;
use think\App;

class Search extends Base
{
    protected $busModel = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busModel = new BusSearch();
    }

    /**
     * 获取所有
     * @return array|\think\response\Json
     */
    public function getSearchByListInfo()
    {
        try {
            $search_list = $this->busModel->getBaseBylistPage([['status','=',1]], 1, 10, "*", 'sort desc,id desc');
            $history_list = (new MallSearchUser())->getBaseBylistPage(['openid' => $this->m_openid,'status'=>1], 1, 10);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        $result = [
            "search"=>$search_list ?? [],
            "history"=>$history_list ?? []
        ];
        return Show::json_show(Status::mini_normal,"ok",$result);
    }

    /**
     * 用户删除搜索关键词
     * @return array|\think\response\Json
     */
    public function delSearchInfo(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::mini_error,"内部错误");
        }
        try {
            $result = (new BusSearchUser())->getDeleteById($id);
        }catch (\Exception $e){
            return Show::json_show(Status::mini_error,$e->getMessage());
        }
        if($result){
            return Show::json_show(Status::mini_normal,"删除成功");
        }else{
            return Show::json_show(Status::mini_error,"删除失败");
        }
    }

    /**
     * @return array|\think\response\Json
     */
    public function delSearchAll(){
        try {
            $result = (new BusSearchUser())->setDeleteAll(['openid'=>$this->m_openid]);
        }catch (\Exception $e){
            return Show::json_show(Status::mini_error,$e->getMessage());
        }
        if($result){
            return Show::json_show(Status::mini_normal,"删除成功");
        }else{
            return Show::json_show(Status::mini_error,"删除失败");
        }
    }
}